//
//  ZHNumberFormatter.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/4/11.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class ZHNumberFormatter: NumberFormatter {

    static func isLegalForPhoneNumber(phoneNumerStr:String)->Bool{
        let phoneStr =  phoneNumerStr.replacingOccurrences(of: " ", with: "")
        guard phoneNumerStr.count > 0 else {
            return false
        }
        let pattern1 = "[0-9]{30}"
        let regex1 = try! NSRegularExpression(pattern: pattern1, options: NSRegularExpression.Options.caseInsensitive)
        let res = regex1.matches(in: phoneStr, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, phoneStr.count))
        return res.count >= 0
    }
    
    override init() {
        super.init()
        self.basicInit()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func basicInit() {
        self.format = "###,###,###,###,###,###"
        self.groupingSeparator = " "
        self.groupingSize = 3  //分隔位数
        self.isLenient = true
        self.numberStyle = .none
        self.paddingCharacter = "0"
        self.paddingPosition = .beforePrefix
        //self.formatWidth = 10
    }
    
    /*
    override func isPartialStringValid(_ partialString: String, newEditingString newString: AutoreleasingUnsafeMutablePointer<NSString?>?, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
        if partialString.isEmpty {
            return true
        }

        if partialString.count > 20{
            return false
        }
        
        return  Int(partialString.replacingOccurrences(of: " ", with: "")) != nil
    }
    */
    
    
}
